//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "h_g.h"

#include "h_portal.h"
#include "h_server.h"

#include "t_mainform.h"
#include "t_confirmform.h"
#include "h_i18n.h"

#include "t_batchform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TBatchForm *BatchForm;
//---------------------------------------------------------------------------
__fastcall TBatchForm::TBatchForm(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
static void __fastcall SplitDailyBatch(const UTF8String &DailyBatch,
   UTF8String &DailyBatch1, UTF8String &DailyBatch2)
{
   vector<string> StrVec;

   SplitString(DailyBatch.c_str(), ',', StrVec);

   DailyBatch1 = "";
   DailyBatch2 = "";

   if ( StrVec.size() >= 2 )
   {
      DailyBatch1 = StrVec[0].c_str();
      DailyBatch2 = StrVec[1].c_str();
   }
}

static UTF8String __fastcall JoinDailyBatch(const UTF8String &DailyBatch1,
   const UTF8String &DailyBatch2)
{
   if ( DailyBatch2 == "" )
   {
      return "";
   }
   else
   {
      return DailyBatch1 + "," + DailyBatch2;
   }
}

static void __fastcall SplitBatchAlarm(const UTF8String &BatchAlarm,
   UTF8String &BatchAlarm1, UTF8String &BatchAlarm2, UTF8String &BatchAlarm3)
{
   vector<string> StrVec;

   SplitString(BatchAlarm.c_str(), ',', StrVec);

   BatchAlarm1 = "";
   BatchAlarm2 = "";
   BatchAlarm3 = "";

   if ( StrVec.size() >= 3 )
   {
      BatchAlarm1 = StrVec[0].c_str();
      BatchAlarm2 = StrVec[1].c_str();
      BatchAlarm3 = StrVec[2].c_str();
   }
   else if ( StrVec.size() >= 2 )
   {
      BatchAlarm1 = StrVec[0].c_str();
      BatchAlarm3 = StrVec[1].c_str();
   }
}

static UTF8String __fastcall JoinBatchAlarm(const UTF8String &BatchAlarm1,
   const UTF8String &BatchAlarm2, const UTF8String &BatchAlarm3)
{
   if ( BatchAlarm3 == "" )
   {
      return "";
   }
   else if ( BatchAlarm2 == "" )
   {
      return BatchAlarm1 + "," + BatchAlarm3;
   }
   else
   {
      return BatchAlarm1 + "," + BatchAlarm2 + "," + BatchAlarm3;
   }
}

void __fastcall TBatchForm::ShowProperties()
{
   HBatchIcon *Batch = MainForm->Portal->CurrentBatch;
   const HBatchOutline &Outline = Batch->Outline;
   Batch->Lock();
   HOutlineStatus OutlineStatus = Batch->OutlineStatus;
   Batch->Unlock();
   EditName->Text = Outline.Name;
   EditCaption->Text = Outline.Caption;

   if ( Outline.HotbackFlag )
   {
      EditIpAddr->Text = Outline.HotbackIpAddr + " (HOTBACK)";
      EditPort->Text = UTF8String(Outline.HotbackPort);
   }
   else
   {
      EditIpAddr->Text = Outline.IpAddr;
      EditPort->Text = UTF8String(Outline.Port);
   }
   EditStatus->Text = OutlineStatus.StatusStr;
   EditStartTime->Text = FormatDateTime(OutlineStatus.StartTime);
   EditEndTime->Text = FormatDateTime(OutlineStatus.EndTime);
   EditElapse->Text = DiffTime(OutlineStatus.StartTime, OutlineStatus.EndTime);
   EditCompletedRatio->Text = OutlineStatus.CompletedRatio;
   EditBanner->Text = OutlineStatus.Banner;

   EditParaJobNum->Text = OutlineStatus.ParaJobNum;

   if( ComboBoxCodeSet->Items->IndexOf(OutlineStatus.CodeSet) == -1 )
      ComboBoxCodeSet->ItemIndex = 0;
   else
      ComboBoxCodeSet->ItemIndex =
         ComboBoxCodeSet->Items->IndexOf(OutlineStatus.CodeSet);

   UTF8String Str1;
   UTF8String Str2;
   UTF8String Str3;

   SplitDailyBatch(OutlineStatus.DailyBatch, Str1, Str2);
   EditDailyBatch1->Text = Str1;
   EditDailyBatch2->Text = Str2;
   SplitBatchAlarm(OutlineStatus.BatchAlarm, Str1, Str2, Str3);
   EditBatchAlarm1->Text = Str1;
   EditBatchAlarm2->Text = Str2;
   EditBatchAlarm3->Text = Str3;
   CheckBoxDedicated->Checked = OutlineStatus.Dedicated;

   EditBatchDate->Text = OutlineStatus.BatchDate;
   EditNextBatchDate->Text = OutlineStatus.NextBatchDate;
   EditEndBatchDate->Text = OutlineStatus.EndBatchDate;
   EditNextScheduleDate->Text = OutlineStatus.NextScheduleDate;

   SetStringToMemo(MemoDescription, Outline.Description);
   SetStringToMemo(MemoCluster, OutlineStatus.Cluster);
   ButtonChangeConfig->Enabled = !Batch->Outline.Offline;
   ButtonChangeDate->Enabled = !Batch->Outline.Offline && Batch->Outline.EnableRestart;
   GroupBox2->Visible = Batch->Outline.ShowBatchDate;
   ShowModal();
}

void __fastcall TBatchForm::ButtonOkClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------
void __fastcall TBatchForm::ButtonChangeConfigClick(TObject *Sender)
{
   HPortal *Portal = MainForm->Portal;

   if ( !ConfirmForm->Confirm(Portal->CurrentBatch,
            h_g::I18nString("t_batchform:CONFIGURE"), "configure") )
   {
      return;
   }

   HBatchIcon *Batch = Portal->CurrentBatch;

   Batch->Lock();
   Batch->OutlineStatus.ParaJobNum = EditParaJobNum->Text.ToIntDef(0);
   Batch->OutlineStatus.CodeSet = ComboBoxCodeSet->Text;
   Batch->OutlineStatus.DailyBatch = JoinDailyBatch(
      EditDailyBatch1->Text, EditDailyBatch2->Text);
   Batch->OutlineStatus.BatchAlarm = JoinBatchAlarm(
      EditBatchAlarm1->Text, EditBatchAlarm2->Text, EditBatchAlarm3->Text);
   Batch->OutlineStatus.Dedicated = CheckBoxDedicated->Checked;
   Batch->Unlock();

   UTF8String TmpStr;

   if ( HServer::FuncChangeConfig(Batch, TmpStr) )
   {
      h_g::ShowMessage(UTF8String(h_g::I18nString("t_batchform:SUCCESS")));
   }
   else
   {
      h_g::ShowMessage(TmpStr);
   }
}
//---------------------------------------------------------------------------

void __fastcall TBatchForm::ButtonChangeDateClick(TObject *Sender)
{
   HPortal *Portal = MainForm->Portal;
   HBatchIcon *Batch = Portal->CurrentBatch;

   {
      Batch->Lock();
      HOutlineStatus OutlineStatus = Batch->OutlineStatus;
      Batch->Unlock();
      UTF8String NewEndDate = EditEndBatchDate->Text.TrimRight();
      if ( NewEndDate != "" && NewEndDate != OutlineStatus.EndBatchDate )
      {
         EditEndBatchDate->Text = OutlineStatus.EndBatchDate;
      }
   }

   if ( !ConfirmForm->Confirm(Portal->CurrentBatch,
            h_g::I18nString("t_batchform:CHANGE_DATE"), "configure") )
   {
      return;
   }

   UTF8String TmpStr;

   if ( HServer::FuncChangeDate(Batch, EditBatchDate->Text,
           EditNextScheduleDate->Text, EditEndBatchDate->Text, TmpStr) )
   {
      Batch->Lock();
      HOutlineStatus OutlineStatus = Batch->OutlineStatus;
      OutlineStatus.BatchDate = EditBatchDate->Text.TrimRight();
      OutlineStatus.NextScheduleDate = EditNextScheduleDate->Text.TrimRight();
      OutlineStatus.EndBatchDate = EditEndBatchDate->Text.TrimRight();
      Batch->OutlineStatus = OutlineStatus;
      Batch->Unlock();
      h_g::ShowMessage(UTF8String(h_g::I18nString("t_batchform:SUCCESS")));
   }
   else
   {
      h_g::ShowMessage(TmpStr);
   }
}
//---------------------------------------------------------------------------
